package com.usian.admin.controller;

import com.usian.admin.NewsAuthControllerApi;
import com.usian.admin.feign.WemediaFeignClient;
import com.usian.model.admin.dtos.NewsAuthDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.media.pojos.WmNews;
import com.usian.model.media.vo.WmNewsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/v1/news_auth")
public class NewsAuthController implements NewsAuthControllerApi {


    @Value("${file.url}")
    private String fileUrl;

    @Autowired
    private WemediaFeignClient weFeignClient;

    @Override
    @PostMapping("/list")
    public ResponseResult findNews(@RequestBody NewsAuthDto dto) {
        return weFeignClient.findList(dto);
    }

    @Override
    @GetMapping("/one/{id}")
    public ResponseResult findOne(@PathVariable("id") Integer id) {
        if (id==null)return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        WmNewsVo wmNewsVo = weFeignClient.findWmNewsVo(id);
        ResponseResult<WmNewsVo> responseResult = new ResponseResult<>();
        responseResult.setData(wmNewsVo);
        responseResult.setHost(fileUrl);
        return responseResult;
    }

    @Override
    @PostMapping("/pass_fail/{type}")
    public ResponseResult passOrFail(@PathVariable("type") Integer type,@RequestBody NewsAuthDto dto) {

        WmNews wm = weFeignClient.findById(dto.getId());
        if (wm==null)return ResponseResult.errorResult("文章不存在");

        if (type.equals(0)){
            wm.setStatus((short) 2);
            wm.setReason(dto.getMsg());
            return weFeignClient.updateWmNews(wm);
        }else if (type.equals(1)){
            wm.setStatus((short) 4);
            wm.setReason(dto.getMsg());
            return weFeignClient.updateWmNews(wm);
        }
        return ResponseResult.errorResult("参数无效");
    }
}
